package io.renren.common.utils;



import io.renren.modules.clearing.entity.DownErrorVersion;
import org.apache.log4j.Logger;
import java.util.Date;


/**
 * 类型转换
 * @author 郑鲁业
 *
 */
public class TypeTransUtil {
	private static final Logger log = Logger.getLogger(TypeTransUtil.class);
	public static  int letterToNumber(String letter) {
        //将String对象中的每一个下标位的对象保存在数组中
        char[] b = letter.toCharArray();
        String id="";
        //转换成响应的ASCLL
        for (char c : b) {
        	if(Integer.valueOf(c)>=48 && Integer.valueOf(c)<=57){
        		id=id+c;
        	}else{
        		id=id+Integer.valueOf(c);
        	}
        }
	    return Integer.valueOf(id);
	}
	
	public static int IDToStationID(String id){
		int stid=0;
		String stationid="";
		try {
			
			if(id.length()==37){
				String roadid =letterToNumber(id.substring(2,5))+"";
				if(roadid.length()>3){
					roadid=roadid.substring(1, 4);
				}
				stationid=roadid+""+Integer.valueOf(id.substring(5,7))+""+id.substring(9,11)+""+id.substring(12,14);
				
			}else if(id.length()==22){
				stationid=id.substring(0,6);
			}
			stid=Integer.valueOf(stationid) ;
		} catch (Exception e) {
			log.info("交易id:"+id+"转换站编码出错");
			//stid=Integer.valueOf("100"+Integer.valueOf(id.substring(5,7))+""+id.substring(12,14));
		}
		
		return stid;
	}
	public static int EnLaneIDToStationID(String id){
		int stid=0;
		String stationid="";
		try {
			if(id.length()==21){
				String roadid =letterToNumber(id.substring(2,5))+"";
				if(roadid.length()>3){
					roadid=roadid.substring(1, 4);
				}
				stationid=roadid+""+Integer.valueOf(id.substring(5,7))+""+id.substring(9,11)+""+id.substring(12,14);
			}else if(id.length()==6){
				stationid=id.substring(0,6);
			}
			stid=Integer.valueOf(stationid) ;
		} catch (Exception e) {
			log.info("交易id:"+id+"转换站编码出错");
			//stid=Integer.valueOf("100"+Integer.valueOf(id.substring(5,7))+""+id.substring(12,14));
		}
		
		return stid;
	}
	/**
	 * 网络编号转换发行方
	 * @param NetID 网络编号
	 * @return 方行发ID
	 */
	public static String netIDToIssueID(String NetID){
		return NetID.substring(0,2)+"01"+NetID.substring(2,4);
	}
	/**
	 * 发行方转换网络编号
	 * @param issueID 发行方ID
	 * @return  网络编号
	 */
	public static String issueIDTonetID(String issueID){
		return issueID.substring(0,2)+issueID.substring(4,6);
	}
	/**
	 * 交易ID获取车道编号
	 * @param id 交易编号=出口收费车道编号+出口交易发生的时间+流水号
	 * @return 营改增车道编码21位
	 */
	public static String transIDToLaneID(String id){
		String laneID="";
		if(id.length()==37){
			laneID=id.substring(0,21);
		}else if(id.length()==22){
			laneID=id.substring(0,6);
		}
		return laneID;
	}
	
	
	/**
	 * 交易ID获取流水序号
	 * @param id 交易编号=出口收费车道编号+出口交易发生的时间+流水号
	 * @return 流水序号2位
	 */
	public static int transIDToExitWasteSN(String id){
		int sn=0;
		if(id.length()==37){
			sn=Integer.valueOf(id.substring(35,37));
		}else if(id.length()==22){
			sn=Integer.valueOf(id.substring(20,22));
		}
		return sn;
	}
	/**
	 * 交易ID获取车道顺序码
	 * @param id 交易编号=出口收费车道编号+出口交易发生的时间+流水号
	 * @return 流水序号2位
	 */
	public static int transIDToExLaneID(String id){
		int laneid=0;
		if(id.length()>=21){
			laneid=Integer.valueOf(id.substring(19,21));
		}else{
			laneid=Integer.valueOf(id.substring(4,6));
		}
		return laneid;
	}
	/**
	 * 根据原收费站编号、车道编号获取营改增车道编码
	 * @param oldstationid 原收费站编号
	 * @param oldlaneid 原车道编号
	 * @return 营改增车道编码
	 *//*
	public static String getYgzlaneID(int oldstationid ,int oldlaneid){
		String laneID="";
		BaseDao baseDao = (BaseDao) Constant.context.getBean("baseDao");
		try {
			laneID=baseDao.getYgzlaneID(oldstationid,oldlaneid);
		} catch (Exception e) {
			e.printStackTrace();
			laneID="-1";
		}
		if(laneID==null || laneID==""){
			laneID="000000000000000000000";
		}
		return laneID;
		
	}*/
	/**
	 * 根据原收费站编号获取营改增车道编码
	 * @param oldstationid 原收费站编号
	 * @return 营改增车道编码
	 */
	/*public static String getYgzlaneIDByStation(int oldstationid){
		String laneID="";
		BaseDao baseDao = (BaseDao) Constant.context.getBean("baseDao");
		try {
			laneID=baseDao.getYgzlaneIDByStation(oldstationid);
		} catch (Exception e) {
			e.printStackTrace();
			laneID="-1";
		}
		if(laneID==null || laneID==""){
			laneID="000000000000000000000";
		}
		return laneID;
		
	}*/
	/**
	 * 根据收费站编号获取收费站名称
	 * @param stationid 收费站编号
	 * @return 营改增车道编码
	 */
	/*public static String getStationName(int stationid){
		String StationName="";
		BaseDao baseDao = (BaseDao) Constant.context.getBean("baseDao");
		try {
			StationName=baseDao.getStationName(stationid);
		} catch (Exception e) {
			e.printStackTrace();
			StationName="";
		}
		return StationName;
		
	}*/
	/**
	 * 根据卡号获取卡类型
	 * @param CardID
	 * @return
	 */
	/*public static int getCardTypeByCardID(String CardID){
		int cardType=0;
		BaseDao baseDao = (BaseDao) Constant.context.getBean("baseDao");
		try {
			cardType=baseDao.getCardTypeByCardID(CardID);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return cardType;
		
	}*/
	/**
	 * 根据时间间隔类型确定当前应该到什么版本
	 * @param type 1：15分钟；2：20分钟;3:1小时;4:1天
	 * @return
	 */
	public static String getNowVersion(int type){
		String version="";
		switch(type){
			case 1:  
				version = DateUtil.getFullDateString3()+CompletionStr.compStr0(DateUtil.getHour()+"",2);
				if(DateUtil.getMinute()>0 && DateUtil.getMinute()<15){
					version=version+"00";
				}else if(DateUtil.getMinute()>15 && DateUtil.getMinute()<30){
					version=version+"15";
				}else if(DateUtil.getMinute()>30 && DateUtil.getMinute()<45){
					version=version+"30";
				}else if(DateUtil.getMinute()>45 && DateUtil.getMinute()<60){
					version=version+"45";
				}
				break;
			case 2: 
				version = DateUtil.getFullDateString3()+CompletionStr.compStr0(DateUtil.getHour()+"",2);
				if(DateUtil.getMinute()>0 && DateUtil.getMinute()<20){
					version=version+"00";
				}else if(DateUtil.getMinute()>20 && DateUtil.getMinute()<40){
					version=version+"20";
				}else if(DateUtil.getMinute()>40 && DateUtil.getMinute()<60){
					version=version+"40";
				}	
				break;
			case 3: 
				version = DateUtil.getFullDateString3()+CompletionStr.compStr0(DateUtil.getHour()+"",2);
				break;
			case 4: //当前日期的前一天
				version = DateUtil.getFullDateString3();
//				version =  DateUtil.getFullDateDay(DateUtil.parseStringToDate(DateUtil.getAddDays(DateUtil.parseDateToString(new Date()), -1)));
			break;
			default:
		}
		return version;
		
	}
	/**
	 * 判断需要处理的版本信息
	 * @param version
	 * @param type
	 * @return
	 */
	public static String[] checkVersion(String version,int type){
		String[] arr = null;
		//间隔15分钟
		if(type==1){
			Date now=DateUtil.parseStringToDateTime3(getNowVersion(type));
			Date end=DateUtil.parseStringToDateTime3(version);
			if(now.compareTo(end)<0){
				log.info("该版本："+version+"配置错误，未到时间");
				return null;
			}
			if(version.equals(getNowVersion(type))){
				arr= new String[1];
				//版本号相同
				arr[0]=version;
			}else{
				//版本号不同判断相差几个版本
				//"201811041500" -"201812041515"
				String sd=version;
				String ed=getNowVersion(type);
				//int day=DateUtil.getDatePoor(DateUtil.parseStringToDateTime3(ed),DateUtil.parseStringToDateTime3(sd),1);
				//int Hour=DateUtil.getDatePoor(DateUtil.parseStringToDateTime3(ed),DateUtil.parseStringToDateTime3(sd),2);
				//int minute=DateUtil.getDatePoor(DateUtil.parseStringToDateTime3(ed),DateUtil.parseStringToDateTime3(sd),3);
				//int diff=day*24*60+Hour*60+minute;
				int diff= DateUtil.getDatePoor(DateUtil.parseStringToDateTime3(ed),DateUtil.parseStringToDateTime3(sd),3);
				
				
				int num =diff/15;
				arr= new String[num+1];
				if(diff>0){
					for (int i = 0; i <= num; i++) {
						arr[i]=DateUtil.getAddMinus(sd, i*15);
					}
				}
				
				
			}
		}else if(type==2){
			
			Date now=DateUtil.parseStringToDateTime3(getNowVersion(type));
			Date end=DateUtil.parseStringToDateTime3(version);
			if(now.compareTo(end)<0){
				log.info("该版本："+version+"配置错误，未到时间");
				return null;
			}
			
			if(version.equals(getNowVersion(type))){
				//版本号相同
				arr= new String[1];
				arr[0]=version;
			}else{
				//版本号不同判断相差几个版本
				//"201811041500" -"201812041520"
				String sd=version;
				String ed=getNowVersion(type);
				//int day=DateUtil.getDatePoor(DateUtil.parseStringToDateTime3(ed),DateUtil.parseStringToDateTime3(sd),1);
				//int Hour=DateUtil.getDatePoor(DateUtil.parseStringToDateTime3(ed),DateUtil.parseStringToDateTime3(sd),2);
				//int minute=DateUtil.getDatePoor(DateUtil.parseStringToDateTime3(ed),DateUtil.parseStringToDateTime3(sd),3);
				//int diff=day*24*60+Hour*60+minute;
				int diff= DateUtil.getDatePoor(DateUtil.parseStringToDateTime3(ed),DateUtil.parseStringToDateTime3(sd),3);
				int num =diff/20;
				arr= new String[num+1];
				if(diff>0){
					for (int i = 0; i <= num; i++) {
						arr[i]=DateUtil.getAddMinus(sd, i*20);
					}
				}
				
			}
		}else if(type==3){
			
			Date now=DateUtil.parseStringToDateTime4(getNowVersion(type));
			Date end=DateUtil.parseStringToDateTime4(version);
			if(now.compareTo(end)<0){
				log.info("该版本："+version+"配置错误，未到时间");
				return null;
			}
			if(version.equals(getNowVersion(type))){
				//版本号相同
				arr= new String[1];
				arr[0]=version;
			}else{
				//版本号不同判断相差几个版本
				//"201811041500" -"201812041520"
				String sd=version;
				String ed=getNowVersion(type);
				//int day=DateUtil.getDatePoor(DateUtil.parseStringToDateTime4(ed),DateUtil.parseStringToDateTime4(sd),1);
				//int Hour=DateUtil.getDatePoor(DateUtil.parseStringToDateTime4(ed),DateUtil.parseStringToDateTime4(sd),2);
				//int diff=day*24+Hour;
				int diff=DateUtil.getDatePoor(DateUtil.parseStringToDateTime4(ed),DateUtil.parseStringToDateTime4(sd),2);
			
				arr= new String[diff+1];
				if(diff>0){
					for (int i = 0; i <= diff; i++) {
						arr[i]=DateUtil.getAddHour(sd, i);
					}
				}
				
			}
		}else if(type==4){
			Date now=DateUtil.parseStringToDateTime5(getNowVersion(type));
			Date end=DateUtil.parseStringToDateTime5(version);
			if(now.compareTo(end)<0){
				log.info("该版本："+version+"配置错误，未到时间");
				return null;
			}
			if(version.equals(getNowVersion(type))){
				//版本号相同
				arr= new String[1];
				arr[0]=version;
			}else{
				//版本号不同判断相差几个版本
				//"20181104" -"20181204"
				String sd=version;
				String ed=getNowVersion(type);
				int day=DateUtil.getDatePoor(DateUtil.parseStringToDateTime5(ed),DateUtil.parseStringToDateTime5(sd),1);
				arr= new String[day+1];
				if(day>0){
					for (int i = 0; i <= day; i++) {
						arr[i]=DateUtil.getAddDays2(sd, i);
					}
				}
				
			}
		}
		return arr;
		
	}
	/**
	 * 根据当前版本获取下一版本
	 * @param version
	 * @param type
	 * @return
	 */
	public static String getNextVersion (String version,int type){
		String nextVersion="";
		if(type==1){
			nextVersion=DateUtil.getAddMinus(version, 15);
		}else if(type==2){
			nextVersion=DateUtil.getAddMinus(version, 20);
		}else if(type==3){
			nextVersion=DateUtil.getAddHour(version, 1);
		}else if(type==4){
			nextVersion=DateUtil.getAddDays2(version, 1);
		}
		return nextVersion;
	}
	
	
	/**
	 * 将本地的卡操作类型转化为传输系统中间表的卡业务类型（0储值1记账转为22储值23记账）
	 * @param type
	 * @return
	 */
	public static int cardTypeFromLocalToMid(int type) {
		int result = -1;
		if(type == 0){ //储值卡业务
			result = 22;
		}else if(type == 1){ //记账卡业务
			result = 23;
		}else{
			
		}
		return result;
	}
	/**
	 * 将本地的卡操作类型转化为传输系统中间表的卡业务类型（22储值23记账转为0储值1记账）
	 * @param type
	 * @return
	 */
	public static int cardTypeFromMidToLocal(int type) {
		int result = -1;
		if(type == 22){ //储值卡业务
			result = 0;
		}else if(type == 23){ //记账卡业务
			result = 1;
		}else{
			
		}
		return result;
	}
	/**
	 * 状态转换
	 * @param transType 卡业务类型，22储值卡业务，23记账卡业务
	 * @param state 中间表记录状态
	 * @return 本地表流水表中的记录状态
	 */
	public static int transStateFromMidToLocal(int transType,int state) {
		int result = 99;
		if(transType == 22){ //储值卡业务
			switch(state){
				case 1:  //应收
					result = 1;
					break;
				case 2:  //确认应收
					result = 1;
					break;		
				case 4:  //待决应收
					result = 0;
					break;
				case 5:  //争议拒付
					result = -1;
					break;
				case 6:  //争议付款
					result = 1;
					break;
				case 12: //确认应收退费
					result = -3;
					break;
				case 16: //争议付款退费
					result = -3;
					break;				
				default:
			}
		}else if(transType == 23){ //记账卡业务
			switch(state){
				case 1:  //应收
					result = 2;
					break;
				case 2:  //确认应收
					result = 2;
					break;		
				case 4:  //待决应收
					result = 0;
					break;
				case 5:  //争议拒付
					result = -1;
					break;
				case 6:  //争议付款
					result = 2;
					break;
				case 12: //确认应收退费
					result = -3;
					break;
				case 16: //争议付款退费
					result = -3;
					break;				
				default:
			}
		}		
		return result;
	}
	/***
	 * 获取车种
	 * @param
	 * @param
	 * @return
	 */
	public static int getVehicleClass(int vtype){
			
			int vehicleClass;
	
			if(vtype==2){//军警车
				vehicleClass = 8;
			}else if(vtype==9){//绿通车
				vehicleClass = 21;
			}else if(vtype==1){//普通收费车
				vehicleClass = 0;
			}else if(vtype==3){//公务车
				vehicleClass = 6;
			}else if(vtype==5){//紧急车
//				vehicleClass = 10;
				vehicleClass = 26;//2019-03-27 根据部中心20190322文档修改  wangyp
			}else if(vtype==4){//车队
				vehicleClass = 14;
			}else{
				vehicleClass = 0;
			}
			
		return vehicleClass;
	}
	
	/***
	 * 获取车种
	 * @param
	 * @param
	 * @return
	 */
	public static int getVtype(int vtype){
			
			int vehicleClass;
	
			if(vtype==8){//军警车
				vehicleClass = 2;
			}else if(vtype==21){//绿通车
				vehicleClass = 9;
			}else if(vtype==0){//普通收费车
				vehicleClass = 1;
			}else if(vtype==6){//公务车
				vehicleClass = 3;
			}else if(vtype==10){//紧急车
				vehicleClass = 5;
			}else if(vtype==14){//车队
				vehicleClass = 4;
			}else if(vtype==26){//应急车 2019-03-27 增加，根据20190322文件修改 wangyp
				vehicleClass = 5;
			}else{
				//vehicleClass = 1;
				vehicleClass = vtype;//按部中心下载的填写
			}
			
		return vehicleClass;
	}
	/**
	 * 字符串转化成为16进制字符串
	 * @param s
	 * @return
	 */
	public static String strTo16(String s) {
	    String str = "";
	    for (int i = 0; i < s.length(); i++) {
	        int ch = (int) s.charAt(i);
	        String s4 = Integer.toHexString(ch);
	        str = str + s4;
	    }
	    return str;
	}
	/**
	 * 填充版本异常白表
	 * @param version 版本号
	 * @param msgType 下载类型
	 * @param reason 原因
	 * @return
	 */
	public static DownErrorVersion addDownErrorVersion(String version, int msgType, String reason ){
		DownErrorVersion dv= new DownErrorVersion();
		dv.setVersion(version);
		dv.setFlag(0);
		dv.setMsgType(msgType);
		dv.setMsgTime("0");
		dv.setReason(reason==null?"-":reason);
		dv.setCreateTime(new Date());
		dv.setSpare1(1);
		return dv;
	}
	
	/**
	 * 获取异常信息
	 * @param Message
	 * @return
	 */
	public static String getErrorMessage(String Message ){
		String msg= "";
		if(Message==null||Message==""){
			msg="空指针异常";
		}else{
			msg=Message.length()>=500?Message.substring(0, 500):Message;
		}
		return msg;
	}

	public static String getYgzlaneIDByStation(Integer entryStation) {
		return null;
	}

	public static String getYgzlaneID(Integer entryStation, Integer entryLane) {
		return null;
	}
	
	/*public static DataExchangeLog getErrorLog(String transID,String messageID,String ResultDesc,String issueid ){
		DataExchangeLog dataLog = new DataExchangeLog();
		dataLog.setWasteSN(new GUID().toString());
		dataLog.setMessageID(messageID);
		dataLog.setTollProvinceId(Config.tollProvinceId);
		dataLog.setIssuerID(issueid);
		dataLog.setSerProvinceID(Config.tollProvinceId);
		dataLog.setProcessTime(new Date());
		dataLog.setMessageClass(-1);
		dataLog.setMessageType(1);
		dataLog.setResult(-1);
		dataLog.setRecCount(0); 
		dataLog.setDisputedCount(0);
		dataLog.setRecAmount(0);
		dataLog.setResultDesc(ResultDesc.length()>500?ResultDesc.substring(0, 500):ResultDesc);
		dataLog.setSpare1(0);
		dataLog.setSpare3(transID);
		return dataLog;
	}*/
	
}
